package org.tigris.jasmincc;

import org.tigris.jasmincc.test.AbstractProductionTest;
import org.tigris.mappedlexer.Lexer;
import org.tigris.mappedlexer.implementation.SimpleLexer;
import org.tigris.mappedlexer.io.implementation.FileCharInputSequence;

/**
 * A template for comparing testing result.
 * 
 * @author Sam Huang
 *
 */
public abstract class ComparisonTestCase extends AbstractProductionTest {

	protected String verifyFileName;

	@Override
	protected void setUp() throws Exception {
		super.setUp();

		verifyFileName = "./test/resources/verify/" + className + ".verify";
		refLexer = new SimpleLexer(new FileCharInputSequence(verifyFileName));
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testParse() throws Exception {
		doParse();
		FileCharInputSequence ss = new FileCharInputSequence(generatedFileName);
		SimpleLexer producedLexer = new SimpleLexer(ss);
		assertEquals(producedLexer, refLexer);
	}

	public static void assertEquals(Lexer lexer, Lexer refLexer) {
		try {
			while (true) {
				logger.info("assert x: " + lexer.nexttok());
				logger.info("assert ref: " + refLexer.nexttok());
				if (!lexer.nexttok().equals(refLexer.nexttok())) {
					assertEquals(refLexer.nexttok(), lexer.nexttok());
				}
			}
		} catch (Exception ex) {
			assertTrue(true);
		}
	}

	public abstract void doParse() throws Exception;
}
